//
//  HDBVersionView.swift
//  HDBWeibo
//
//  Created by leidon on 2016/11/24.
//  Copyright © 2016年 BONC. All rights reserved.
//

import UIKit


/// 访客模式  把单词写错了 version -> visitor
class HDBVersionView: UIView {

    
    /// didSet 让外界传入数据 
    //[String : String]? Home页面的“imageName”设置为nil的
    var versionInfo : [String : String]?{
        didSet{
            guard let imageName = versionInfo?["imageName"],
                let introduce = versionInfo?["introduce"] else {
                    return
            }
            
            introduceLabel.text = introduce
            
            //Home页面的imageName是“”，当imageName = “”的时候，结束这个函数
            if imageName == "" {
                
                addCircleAnimation()
                
                return
            }
            
            coverView.isHidden = true
            houseView.isHidden = true
            
            mainView.image = UIImage(named : imageName)
        }
    }
    
    
    override init(frame : CGRect){
        super.init(frame: frame)
        
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    //MARK: 构建每一组模型  将访客模式的控件都放进来 进行懒加载
    
    //遮盖图片
    lazy var coverView : UIImageView = UIImageView(image : UIImage(named : "visitordiscover_feed_mask_smallicon"))
    
    //小圈圈 - UIImageView
    lazy var mainView : UIImageView = UIImageView(image : UIImage(named : "visitordiscover_feed_image_smallicon"))
    
    //大背景小房子 - UIImageView
    lazy var houseView : UIImageView = UIImageView(image : UIImage(named : "visitordiscover_feed_image_house"))
    
    //文字介绍 - UILabel
    lazy var introduceLabel : UILabel = UILabel.cz_label(withText: "蒹葭苍苍，白露为霜，所谓伊人，在水一方;所谓伊人，在水之湄;所谓伊人，摇手可见；蒹葭采采，蒹葭萋萋", fontSize: 14, color: UIColor.darkGray)
    
    //登录按钮 - UIButton
    lazy var loginBtn : UIButton = UIButton.cz_textButton("登录", fontSize: 14, normalColor: UIColor.black, highlightedColor: UIColor.orange, backgroundImageName: "common_button_white_disable")
    
    //注册按钮 - UIButton
    lazy var registerBtn : UIButton = UIButton.cz_textButton("注册", fontSize: 16, normalColor: UIColor.orange, highlightedColor: UIColor.black, backgroundImageName: "common_button_white_disable")
    
    
    /// 设置访客视图信息  //这个方法不用，以didSet的方式，重写一遍
    ///
    /// - parameter dict: dict[imageName\introduce]
    private func setupVersionInfo(dict:[String:String]?){
    
        guard let imageName = dict?["iamgeName"],
              let introduce = dict?["introduce"] else {
            return
        }
        
        introduceLabel.text = introduce
        
        //Home页面的imageName是“”，当imageName = “”的时候，结束这个函数
        if imageName == "" {
            return
        }
        
        mainView.image = UIImage(named : imageName)
        
    }
}
//MARK: 设置控件
extension HDBVersionView{
    
    /// 给circle添加旋转动画
    func addCircleAnimation(){
        let anim = CABasicAnimation(keyPath: "transform.rotation")
        
        anim.toValue = 2 * M_PI
        anim.repeatCount = MAXFLOAT
        anim.duration = 15
        
        //动画完成不删除，如果mainView释放，动画被一起销毁 ： 作用是 切换页面  推出重登事动画不停止
        anim.isRemovedOnCompletion = false
        
        mainView.layer.add(anim, forKey: nil)
        
    }
    
    /// 布局
    func setupUI() {
        backgroundColor = UIColor.cz_color(withHex: 0xEDEDED)
//        backgroundColor = UIColor.white
        //1 添加控件
        addSubview(mainView)
        addSubview(coverView) //cricleView之上，houseView之下
        addSubview(houseView)
        addSubview(introduceLabel)
        addSubview(registerBtn)
        addSubview(loginBtn)
        
        introduceLabel.textAlignment = .center
        
        //2 拒绝Autoresizing
        for v in subviews{
            v.translatesAutoresizingMaskIntoConstraints = false
        }
        
        //3 自动布局
        //小间距 ： 20
        let spacing : CGFloat = 20.0
        //Button 的宽度 ：
        let widthBtn : CGFloat = 100.0
        
        //小圆圈 - cricleView
        addConstraint(NSLayoutConstraint(item: mainView,
                                         attribute: .centerX,
                                         relatedBy: .equal,
                                         toItem: self,
                                         attribute: .centerX,
                                         multiplier: 1.0,
                                         constant: 0))
        
        addConstraint(NSLayoutConstraint(item: mainView,
                                         attribute: .centerY,
                                         relatedBy: .equal,
                                         toItem: self,
                                         attribute: .centerY,
                                         multiplier: 1.0,
                                         constant: -100))
        
        //小房纸 - houseView
        addConstraint(NSLayoutConstraint(item: houseView,
                                         attribute: .centerX,
                                         relatedBy: .equal,
                                         toItem: mainView,
                                         attribute: .centerX,
                                         multiplier: 1.0,
                                         constant: 0))
        
        addConstraint(NSLayoutConstraint(item: houseView,
                                         attribute: .centerY,
                                         relatedBy: .equal,
                                         toItem: mainView,
                                         attribute: .centerY,
                                         multiplier: 1.0,
                                         constant: 0))
        
        //文字介绍 - introduceLabel
        addConstraint(NSLayoutConstraint(item: introduceLabel,
                                         attribute: .centerX,
                                         relatedBy: .equal,
                                         toItem: mainView,
                                         attribute: .centerX,
                                         multiplier: 1.0,
                                         constant: 0))
        
        addConstraint(NSLayoutConstraint(item: introduceLabel,
                                         attribute: .centerY,
                                         relatedBy: .equal,
                                         toItem: mainView,
                                         attribute: .bottom,
                                         multiplier: 1.0,
                                         constant: spacing))
        
        addConstraint(NSLayoutConstraint(item: introduceLabel,
                                        attribute: .width,
                                        relatedBy: .equal,
                                        toItem: nil,
                                        attribute: .notAnAttribute,
                                        multiplier: 1.0,
                                        constant: 236))

        //注册 - registerBtn 
        addConstraint(NSLayoutConstraint(item: registerBtn,
                                         attribute: .left,
                                         relatedBy: .equal,
                                         toItem: introduceLabel,
                                         attribute: .left,
                                         multiplier: 1.0,
                                         constant: 0))
        
        addConstraint(NSLayoutConstraint(item: registerBtn,
                                         attribute: .centerY,
                                         relatedBy: .equal,
                                         toItem: introduceLabel,
                                         attribute: .bottom,
                                         multiplier: 1.0,
                                         constant: spacing))

        addConstraint(NSLayoutConstraint(item: registerBtn,
                                         attribute: .width,
                                         relatedBy: .equal,
                                         toItem: nil,
                                         attribute: .notAnAttribute,
                                         multiplier: 1.0,
                                         constant: widthBtn))
        //登录 - loginBtn
        addConstraint(NSLayoutConstraint(item: loginBtn,
                                         attribute: .right,
                                         relatedBy: .equal,
                                         toItem: introduceLabel,
                                         attribute: .right,
                                         multiplier: 1.0,
                                         constant: 0))
        
        addConstraint(NSLayoutConstraint(item: loginBtn,
                                         attribute: .centerY,
                                         relatedBy: .equal,
                                         toItem: introduceLabel,
                                         attribute: .bottom,
                                         multiplier: 1.0,
                                         constant: spacing))
        
        addConstraint(NSLayoutConstraint(item: loginBtn,
                                         attribute: .width,
                                         relatedBy: .equal,
                                         toItem: nil,
                                         attribute: .notAnAttribute,
                                         multiplier: 1.0,
                                         constant: widthBtn))

        //遮盖图片 - coverView  
        //定义VFL中控件名称和实际名称的映射关系
        let dict = ["coverView" : coverView,
                    "loginBtn" : loginBtn] as [String : Any]
        let metrics = ["spacing" : -40]
        addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "H:|-0-[coverView]-0-|",
                                                      options: [],
                                                      metrics: nil,
                                                      views: dict))

        addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "V:|-0-[coverView]-(spacing)-[loginBtn]",
                                                      options: [],
                                                      metrics: metrics,
                                                      views: dict))
    }
    
    
}
